package com.zhuangqi.algorithm.base;

import com.zhuangqi.algorithm.template.BaseTemplate;

import java.util.Arrays;

import static com.zhuangqi.algorithm.template.BaseTemplate.*;

/**
 * 选择排序
 *
 * 6 3 7 2 8
 * 3 6 7 2 8
 * 2 6 7 3 8
 * 2 3 6 7 8
 *
 */
public class Selection {

    public static void sort(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            //最小元素的索引
            int min = i;
            for (int j = i+1; j < a.length; j++) {
                if (less(a[j],a[min])) {
                    //j比min还小，改变最小元素索引
                    min = j;
                }
            }
            //最小元素前移
            exch(a,i,min);
        }
    }

    public static void main(String[] args) {
        Integer[] a = {4,6,7,8,1,5,10,2,8,3};
        Selection.sort(a);
        System.out.println(Arrays.toString(a));
    }

}
